home *** CD-ROM | disk | FTP | other *** search
- /* fastdemo.c: jl 5 May 1986: demonstrate DES in various modes, using */
- /* optimized DES version */
- /* changes 2 June 1986: */
- /* use Dan Hoey's IP, IP-1 optimization */
-
- /* define TIMEONLY to time DES cycles rather than run ordinary demo */
- /* timing data on C/70, (compiled -O) with 500 iterations */
- /* (each iteration is an encrypt+decrypt) */
- /* without HOEYOPT: typical DES op is about 7.825 msec (470 ticks/1000 opns) */
- /* with HOEYOPT: typical DES op is about 7.425 msec (445 ticks/1000 opns) */
- /* this compares with VAX timing of a slightly different version, */
- /* (pre-HOEYOPT) of 2.6 ms/cycle */
-
- /* Additional timing data, from David Balenson at NBS, March 1988 */
- /* I initially tried it on our VAX 11/750 and SUN 3/160 (both */
- /* running UNIX BSD 4.2) and acheived measurements of 15.3Kbps on the */
- /* VAX and 54.1Kbps(!!!) on the SUN. It also compiled and ran successfully */
- /* under PC-DOS using the Computer Innovations C86 C compiler. I added */
- /* DOS-based timing routines in place of your UNIX routines and got times */
- /* of 8.4Kbps on an 8MHz AT and 10.2Kbps on a PS/2-50. */
-
- #include <stdio.h>
- #include <ctype.h>
-
- #define FALSE 0
- #define TRUE 1
-
- #define ECB 1
- #define CBC ECB+1
- #define OFB CBC+1
- #define CFB OFB+1
-
- /* Dan Hoey (hoey@nrl-aic)'s optimization, per 16 May 1986 netmail */
- /* Exchange bits of A selected by mask M with bits of B selected by */
- /* mask (M << S). TMP is a temporary. */
- #define EXSHMSK(A,M,B,S,TMP) \
- TMP = ((B >> S) ^ A) & M; \
- A ^= TMP; \
- TMP <<= S; \
- B ^= TMP
-
- /* tables to describe permutations, per NBS FIPS */
-
- static int pc1c [] =
- { 57, 49, 41, 33, 25, 17, 9,
- 1, 58, 50, 42, 34, 26, 18,
- 10, 2, 59, 51, 43, 35, 27,
- 19, 11, 3, 60, 52, 44, 36 };
-
- static int pc1d [] =
- { 63, 55, 47, 39, 31, 23, 15,
- 7, 62, 54, 46, 38, 30, 22,
- 14, 6, 61, 53, 45, 37, 29,
- 21, 13, 5, 28, 20, 12, 4 };
-
- static int shiftsked []=
- { 1, 1, 2, 2, 2, 2, 2, 2,
- 1, 2, 2, 2, 2, 2, 2, 1 };
-
- #ifdef TIMEONLY
- /* facilities for timing */
- struct tbuffer
- {
- long proc_utime;
- long proc_stime;
- long child_utime;
- long child_stime;
- };
- struct tbuffer t_before;
- struct tbuffer t_after;
- #endif
-
- long snop [8] [64] = {
- 0X808200L,0X0L,0X8000L,0X808202L,
- 0X808002L,0X8202L,0X2L,0X8000L,
- 0X200L,0X808200L,0X808202L,0X200L,
- 0X800202L,0X808002L,0X800000L,0X2L,
- 0X202L,0X800200L,0X800200L,0X8200L,
- 0X8200L,0X808000L,0X808000L,0X800202L,
- 0X8002L,0X800002L,0X800002L,0X8002L,
- 0X0L,0X202L,0X8202L,0X800000L,
- 0X8000L,0X808202L,0X2L,0X808000L,
- 0X808200L,0X800000L,0X800000L,0X200L,
- 0X808002L,0X8000L,0X8200L,0X800002L,
- 0X200L,0X2L,0X800202L,0X8202L,
- 0X808202L,0X8002L,0X808000L,0X800202L,
- 0X800002L,0X202L,0X8202L,0X808200L,
- 0X202L,0X800200L,0X800200L,0X0L,
- 0X8002L,0X8200L,0X0L,0X808002L,
-
- 0X104L,0X4010100L,0X0L,0X4010004L,
- 0X4000100L,0X0L,0X10104L,0X4000100L,
- 0X10004L,0X4000004L,0X4000004L,0X10000L,
- 0X4010104L,0X10004L,0X4010000L,0X104L,
- 0X4000000L,0X4L,0X4010100L,0X100L,
- 0X10100L,0X4010000L,0X4010004L,0X10104L,
- 0X4000104L,0X10100L,0X10000L,0X4000104L,
- 0X4L,0X4010104L,0X100L,0X4000000L,
- 0X4010100L,0X4000000L,0X10004L,0X104L,
- 0X10000L,0X4010100L,0X4000100L,0X0L,
- 0X100L,0X10004L,0X4010104L,0X4000100L,
- 0X4000004L,0X100L,0X0L,0X4010004L,
- 0X4000104L,0X10000L,0X4000000L,0X4010104L,
- 0X4L,0X10104L,0X10100L,0X4000004L,
- 0X4010000L,0X4000104L,0X104L,0X4010000L,
- 0X10104L,0X4L,0X4010004L,0X10100L,
-
- 0X80L,0X1040080L,0X1040000L,0X21000080L,
- 0X40000L,0X80L,0X20000000L,0X1040000L,
- 0X20040080L,0X40000L,0X1000080L,0X20040080L,
- 0X21000080L,0X21040000L,0X40080L,0X20000000L,
- 0X1000000L,0X20040000L,0X20040000L,0X0L,
- 0X20000080L,0X21040080L,0X21040080L,0X1000080L,
- 0X21040000L,0X20000080L,0X0L,0X21000000L,
- 0X1040080L,0X1000000L,0X21000000L,0X40080L,
- 0X40000L,0X21000080L,0X80L,0X1000000L,
- 0X20000000L,0X1040000L,0X21000080L,0X20040080L,
- 0X1000080L,0X20000000L,0X21040000L,0X1040080L,
- 0X20040080L,0X80L,0X1000000L,0X21040000L,
- 0X21040080L,0X40080L,0X21000000L,0X21040080L,
- 0X1040000L,0X0L,0X20040000L,0X21000000L,
- 0X40080L,0X1000080L,0X20000080L,0X40000L,
- 0X0L,0X20040000L,0X1040080L,0X20000080L,
-
- 0X100000L,0X2100001L,0X2000401L,0X0L,
- 0X400L,0X2000401L,0X100401L,0X2100400L,
- 0X2100401L,0X100000L,0X0L,0X2000001L,
- 0X1L,0X2000000L,0X2100001L,0X401L,
- 0X2000400L,0X100401L,0X100001L,0X2000400L,
- 0X2000001L,0X2100000L,0X2100400L,0X100001L,
- 0X2100000L,0X400L,0X401L,0X2100401L,
- 0X100400L,0X1L,0X2000000L,0X100400L,
- 0X2000000L,0X100400L,0X100000L,0X2000401L,
- 0X2000401L,0X2100001L,0X2100001L,0X1L,
- 0X100001L,0X2000000L,0X2000400L,0X100000L,
- 0X2100400L,0X401L,0X100401L,0X2100400L,
- 0X401L,0X2000001L,0X2100401L,0X2100000L,
- 0X100400L,0X0L,0X1L,0X2100401L,
- 0X0L,0X100401L,0X2100000L,0X400L,
- 0X2000001L,0X2000400L,0X400L,0X100001L,
-
- 0X40084010L,0X40004000L,0X4000L,0X84010L,
- 0X80000L,0X10L,0X40080010L,0X40004010L,
- 0X40000010L,0X40084010L,0X40084000L,0X40000000L,
- 0X40004000L,0X80000L,0X10L,0X40080010L,
- 0X84000L,0X80010L,0X40004010L,0X0L,
- 0X40000000L,0X4000L,0X84010L,0X40080000L,
- 0X80010L,0X40000010L,0X0L,0X84000L,
- 0X4010L,0X40084000L,0X40080000L,0X4010L,
- 0X0L,0X84010L,0X40080010L,0X80000L,
- 0X40004010L,0X40080000L,0X40084000L,0X4000L,
- 0X40080000L,0X40004000L,0X10L,0X40084010L,
- 0X84010L,0X10L,0X4000L,0X40000000L,
- 0X4010L,0X40084000L,0X80000L,0X40000010L,
- 0X80010L,0X40004010L,0X40000010L,0X80010L,
- 0X84000L,0X0L,0X40004000L,0X4010L,
- 0X40000000L,0X40080010L,0X40084010L,0X84000L,
-
- 0X80401000L,0X80001040L,0X80001040L,0X40L,
- 0X401040L,0X80400040L,0X80400000L,0X80001000L,
- 0X0L,0X401000L,0X401000L,0X80401040L,
- 0X80000040L,0X0L,0X400040L,0X80400000L,
- 0X80000000L,0X1000L,0X400000L,0X80401000L,
- 0X40L,0X400000L,0X80001000L,0X1040L,
- 0X80400040L,0X80000000L,0X1040L,0X400040L,
- 0X1000L,0X401040L,0X80401040L,0X80000040L,
- 0X400040L,0X80400000L,0X401000L,0X80401040L,
- 0X80000040L,0X0L,0X0L,0X401000L,
- 0X1040L,0X400040L,0X80400040L,0X80000000L,
- 0X80401000L,0X80001040L,0X80001040L,0X40L,
- 0X80401040L,0X80000040L,0X80000000L,0X1000L,
- 0X80400000L,0X80001000L,0X401040L,0X80400040L,
- 0X80001000L,0X1040L,0X400000L,0X80401000L,
- 0X40L,0X400000L,0X1000L,0X401040L,
-
- 0X10000008L,0X10200000L,0X2000L,0X10202008L,
- 0X10200000L,0X8L,0X10202008L,0X200000L,
- 0X10002000L,0X202008L,0X200000L,0X10000008L,
- 0X200008L,0X10002000L,0X10000000L,0X2008L,
- 0X0L,0X200008L,0X10002008L,0X2000L,
- 0X202000L,0X10002008L,0X8L,0X10200008L,
- 0X10200008L,0X0L,0X202008L,0X10202000L,
- 0X2008L,0X202000L,0X10202000L,0X10000000L,
- 0X10002000L,0X8L,0X10200008L,0X202000L,
- 0X10202008L,0X200000L,0X2008L,0X10000008L,
- 0X200000L,0X10002000L,0X10000000L,0X2008L,
- 0X10000008L,0X10202008L,0X202000L,0X10200000L,
- 0X202008L,0X10202000L,0X0L,0X10200008L,
- 0X8L,0X2000L,0X10200000L,0X202008L,
- 0X2000L,0X200008L,0X10002008L,0X0L,
- 0X10202000L,0X10000000L,0X200008L,0X10002008L,
-
- 0X8000820L,0X800L,0X20000L,0X8020820L,
- 0X8000000L,0X8000820L,0X20L,0X8000000L,
- 0X20020L,0X8020000L,0X8020820L,0X20800L,
- 0X8020800L,0X20820L,0X800L,0X20L,
- 0X8020000L,0X8000020L,0X8000800L,0X820L,
- 0X20800L,0X20020L,0X8020020L,0X8020800L,
- 0X820L,0X0L,0X0L,0X8020020L,
- 0X8000020L,0X8000800L,0X20820L,0X20000L,
- 0X20820L,0X20000L,0X8020800L,0X800L,
- 0X20L,0X8020020L,0X800L,0X20820L,
- 0X8000800L,0X20L,0X8000020L,0X8020000L,
- 0X8020020L,0X8000000L,0X20000L,0X8000820L,
- 0X0L,0X8020820L,0X20020L,0X8000020L,
- 0X8020000L,0X8000800L,0X8000820L,0X0L,
- 0X8020820L,0X20800L,0X20800L,0X820L,
- 0X820L,0X20020L,0X8000000L,0X8020800L
- };
-
- long pc2otab [8] [128] = {
- 0X0L,0X10L,0X4000L,0X4010L,0X40000L,0X40010L,0X44000L,0X44010L,
- 0X100L,0X110L,0X4100L,0X4110L,0X40100L,0X40110L,0X44100L,0X44110L,
- 0X20000L,0X20010L,0X24000L,0X24010L,0X60000L,0X60010L,0X64000L,0X64010L,
- 0X20100L,0X20110L,0X24100L,0X24110L,0X60100L,0X60110L,0X64100L,0X64110L,
- 0X1L,0X11L,0X4001L,0X4011L,0X40001L,0X40011L,0X44001L,0X44011L,
- 0X101L,0X111L,0X4101L,0X4111L,0X40101L,0X40111L,0X44101L,0X44111L,
- 0X20001L,0X20011L,0X24001L,0X24011L,0X60001L,0X60011L,0X64001L,0X64011L,
- 0X20101L,0X20111L,0X24101L,0X24111L,0X60101L,0X60111L,0X64101L,0X64111L,
- 0X80000L,0X80010L,0X84000L,0X84010L,0XC0000L,0XC0010L,0XC4000L,0XC4010L,
- 0X80100L,0X80110L,0X84100L,0X84110L,0XC0100L,0XC0110L,0XC4100L,0XC4110L,
- 0XA0000L,0XA0010L,0XA4000L,0XA4010L,0XE0000L,0XE0010L,0XE4000L,0XE4010L,
- 0XA0100L,0XA0110L,0XA4100L,0XA4110L,0XE0100L,0XE0110L,0XE4100L,0XE4110L,
- 0X80001L,0X80011L,0X84001L,0X84011L,0XC0001L,0XC0011L,0XC4001L,0XC4011L,
- 0X80101L,0X80111L,0X84101L,0X84111L,0XC0101L,0XC0111L,0XC4101L,0XC4111L,
- 0XA0001L,0XA0011L,0XA4001L,0XA4011L,0XE0001L,0XE0011L,0XE4001L,0XE4011L,
- 0XA0101L,0XA0111L,0XA4101L,0XA4111L,0XE0101L,0XE0111L,0XE4101L,0XE4111L,
-
- 0X0L,0X800000L,0X2L,0X800002L,0X200L,0X800200L,0X202L,0X800202L,
- 0X200000L,0XA00000L,0X200002L,0XA00002L,0X200200L,0XA00200L,0X200202L,0XA00202L,
- 0X1000L,0X801000L,0X1002L,0X801002L,0X1200L,0X801200L,0X1202L,0X801202L,
- 0X201000L,0XA01000L,0X201002L,0XA01002L,0X201200L,0XA01200L,0X201202L,0XA01202L,
- 0X0L,0X800000L,0X2L,0X800002L,0X200L,0X800200L,0X202L,0X800202L,
- 0X200000L,0XA00000L,0X200002L,0XA00002L,0X200200L,0XA00200L,0X200202L,0XA00202L,
- 0X1000L,0X801000L,0X1002L,0X801002L,0X1200L,0X801200L,0X1202L,0X801202L,
- 0X201000L,0XA01000L,0X201002L,0XA01002L,0X201200L,0XA01200L,0X201202L,0XA01202L,
- 0X40L,0X800040L,0X42L,0X800042L,0X240L,0X800240L,0X242L,0X800242L,
- 0X200040L,0XA00040L,0X200042L,0XA00042L,0X200240L,0XA00240L,0X200242L,0XA00242L,
- 0X1040L,0X801040L,0X1042L,0X801042L,0X1240L,0X801240L,0X1242L,0X801242L,
- 0X201040L,0XA01040L,0X201042L,0XA01042L,0X201240L,0XA01240L,0X201242L,0XA01242L,
- 0X40L,0X800040L,0X42L,0X800042L,0X240L,0X800240L,0X242L,0X800242L,
- 0X200040L,0XA00040L,0X200042L,0XA00042L,0X200240L,0XA00240L,0X200242L,0XA00242L,
- 0X1040L,0X801040L,0X1042L,0X801042L,0X1240L,0X801240L,0X1242L,0X801242L,
- 0X201040L,0XA01040L,0X201042L,0XA01042L,0X201240L,0XA01240L,0X201242L,0XA01242L,
-
- 0X0L,0X2000L,0X4L,0X2004L,0X400L,0X2400L,0X404L,0X2404L,
- 0X0L,0X2000L,0X4L,0X2004L,0X400L,0X2400L,0X404L,0X2404L,
- 0X400000L,0X402000L,0X400004L,0X402004L,0X400400L,0X402400L,0X400404L,0X402404L,
- 0X400000L,0X402000L,0X400004L,0X402004L,0X400400L,0X402400L,0X400404L,0X402404L,
- 0X20L,0X2020L,0X24L,0X2024L,0X420L,0X2420L,0X424L,0X2424L,
- 0X20L,0X2020L,0X24L,0X2024L,0X420L,0X2420L,0X424L,0X2424L,
- 0X400020L,0X402020L,0X400024L,0X402024L,0X400420L,0X402420L,0X400424L,0X402424L,
- 0X400020L,0X402020L,0X400024L,0X402024L,0X400420L,0X402420L,0X400424L,0X402424L,
- 0X8000L,0XA000L,0X8004L,0XA004L,0X8400L,0XA400L,0X8404L,0XA404L,
- 0X8000L,0XA000L,0X8004L,0XA004L,0X8400L,0XA400L,0X8404L,0XA404L,
- 0X408000L,0X40A000L,0X408004L,0X40A004L,0X408400L,0X40A400L,0X408404L,0X40A404L,
- 0X408000L,0X40A000L,0X408004L,0X40A004L,0X408400L,0X40A400L,0X408404L,0X40A404L,
- 0X8020L,0XA020L,0X8024L,0XA024L,0X8420L,0XA420L,0X8424L,0XA424L,
- 0X8020L,0XA020L,0X8024L,0XA024L,0X8420L,0XA420L,0X8424L,0XA424L,
- 0X408020L,0X40A020L,0X408024L,0X40A024L,0X408420L,0X40A420L,0X408424L,0X40A424L,
- 0X408020L,0X40A020L,0X408024L,0X40A024L,0X408420L,0X40A420L,0X408424L,0X40A424L,
-
- 0X0L,0X10000L,0X8L,0X10008L,0X80L,0X10080L,0X88L,0X10088L,
- 0X0L,0X10000L,0X8L,0X10008L,0X80L,0X10080L,0X88L,0X10088L,
- 0X100000L,0X110000L,0X100008L,0X110008L,0X100080L,0X110080L,0X100088L,0X110088L,
- 0X100000L,0X110000L,0X100008L,0X110008L,0X100080L,0X110080L,0X100088L,0X110088L,
- 0X800L,0X10800L,0X808L,0X10808L,0X880L,0X10880L,0X888L,0X10888L,
- 0X800L,0X10800L,0X808L,0X10808L,0X880L,0X10880L,0X888L,0X10888L,
- 0X100800L,0X110800L,0X100808L,0X110808L,0X100880L,0X110880L,0X100888L,0X110888L,
- 0X100800L,0X110800L,0X100808L,0X110808L,0X100880L,0X110880L,0X100888L,0X110888L,
- 0X0L,0X10000L,0X8L,0X10008L,0X80L,0X10080L,0X88L,0X10088L,
- 0X0L,0X10000L,0X8L,0X10008L,0X80L,0X10080L,0X88L,0X10088L,
- 0X100000L,0X110000L,0X100008L,0X110008L,0X100080L,0X110080L,0X100088L,0X110088L,
- 0X100000L,0X110000L,0X100008L,0X110008L,0X100080L,0X110080L,0X100088L,0X110088L,
- 0X800L,0X10800L,0X808L,0X10808L,0X880L,0X10880L,0X888L,0X10888L,
- 0X800L,0X10800L,0X808L,0X10808L,0X880L,0X10880L,0X888L,0X10888L,
- 0X100800L,0X110800L,0X100808L,0X110808L,0X100880L,0X110880L,0X100888L,0X110888L,
- 0X100800L,0X110800L,0X100808L,0X110808L,0X100880L,0X110880L,0X100888L,0X110888L,
-
- 0X0L,0X0L,0X80L,0X80L,0X2000L,0X2000L,0X2080L,0X2080L,
- 0X1L,0X1L,0X81L,0X81L,0X2001L,0X2001L,0X2081L,0X2081L,
- 0X200000L,0X200000L,0X200080L,0X200080L,0X202000L,0X202000L,0X202080L,0X202080L,
- 0X200001L,0X200001L,0X200081L,0X200081L,0X202001L,0X202001L,0X202081L,0X202081L,
- 0X20000L,0X20000L,0X20080L,0X20080L,0X22000L,0X22000L,0X22080L,0X22080L,
- 0X20001L,0X20001L,0X20081L,0X20081L,0X22001L,0X22001L,0X22081L,0X22081L,
- 0X220000L,0X220000L,0X220080L,0X220080L,0X222000L,0X222000L,0X222080L,0X222080L,
- 0X220001L,0X220001L,0X220081L,0X220081L,0X222001L,0X222001L,0X222081L,0X222081L,
- 0X2L,0X2L,0X82L,0X82L,0X2002L,0X2002L,0X2082L,0X2082L,
- 0X3L,0X3L,0X83L,0X83L,0X2003L,0X2003L,0X2083L,0X2083L,
- 0X200002L,0X200002L,0X200082L,0X200082L,0X202002L,0X202002L,0X202082L,0X202082L,
- 0X200003L,0X200003L,0X200083L,0X200083L,0X202003L,0X202003L,0X202083L,0X202083L,
- 0X20002L,0X20002L,0X20082L,0X20082L,0X22002L,0X22002L,0X22082L,0X22082L,
- 0X20003L,0X20003L,0X20083L,0X20083L,0X22003L,0X22003L,0X22083L,0X22083L,
- 0X220002L,0X220002L,0X220082L,0X220082L,0X222002L,0X222002L,0X222082L,0X222082L,
- 0X220003L,0X220003L,0X220083L,0X220083L,0X222003L,0X222003L,0X222083L,0X222083L,
-
- 0X0L,0X10L,0X800000L,0X800010L,0X10000L,0X10010L,0X810000L,0X810010L,
- 0X200L,0X210L,0X800200L,0X800210L,0X10200L,0X10210L,0X810200L,0X810210L,
- 0X0L,0X10L,0X800000L,0X800010L,0X10000L,0X10010L,0X810000L,0X810010L,
- 0X200L,0X210L,0X800200L,0X800210L,0X10200L,0X10210L,0X810200L,0X810210L,
- 0X100000L,0X100010L,0X900000L,0X900010L,0X110000L,0X110010L,0X910000L,0X910010L,
- 0X100200L,0X100210L,0X900200L,0X900210L,0X110200L,0X110210L,0X910200L,0X910210L,
- 0X100000L,0X100010L,0X900000L,0X900010L,0X110000L,0X110010L,0X910000L,0X910010L,
- 0X100200L,0X100210L,0X900200L,0X900210L,0X110200L,0X110210L,0X910200L,0X910210L,
- 0X4L,0X14L,0X800004L,0X800014L,0X10004L,0X10014L,0X810004L,0X810014L,
- 0X204L,0X214L,0X800204L,0X800214L,0X10204L,0X10214L,0X810204L,0X810214L,
- 0X4L,0X14L,0X800004L,0X800014L,0X10004L,0X10014L,0X810004L,0X810014L,
- 0X204L,0X214L,0X800204L,0X800214L,0X10204L,0X10214L,0X810204L,0X810214L,
- 0X100004L,0X100014L,0X900004L,0X900014L,0X110004L,0X110014L,0X910004L,0X910014L,
- 0X100204L,0X100214L,0X900204L,0X900214L,0X110204L,0X110214L,0X910204L,0X910214L,
- 0X100004L,0X100014L,0X900004L,0X900014L,0X110004L,0X110014L,0X910004L,0X910014L,
- 0X100204L,0X100214L,0X900204L,0X900214L,0X110204L,0X110214L,0X910204L,0X910214L,
-
- 0X0L,0X400L,0X1000L,0X1400L,0X80000L,0X80400L,0X81000L,0X81400L,
- 0X20L,0X420L,0X1020L,0X1420L,0X80020L,0X80420L,0X81020L,0X81420L,
- 0X4000L,0X4400L,0X5000L,0X5400L,0X84000L,0X84400L,0X85000L,0X85400L,
- 0X4020L,0X4420L,0X5020L,0X5420L,0X84020L,0X84420L,0X85020L,0X85420L,
- 0X800L,0XC00L,0X1800L,0X1C00L,0X80800L,0X80C00L,0X81800L,0X81C00L,
- 0X820L,0XC20L,0X1820L,0X1C20L,0X80820L,0X80C20L,0X81820L,0X81C20L,
- 0X4800L,0X4C00L,0X5800L,0X5C00L,0X84800L,0X84C00L,0X85800L,0X85C00L,
- 0X4820L,0X4C20L,0X5820L,0X5C20L,0X84820L,0X84C20L,0X85820L,0X85C20L,
- 0X0L,0X400L,0X1000L,0X1400L,0X80000L,0X80400L,0X81000L,0X81400L,
- 0X20L,0X420L,0X1020L,0X1420L,0X80020L,0X80420L,0X81020L,0X81420L,
- 0X4000L,0X4400L,0X5000L,0X5400L,0X84000L,0X84400L,0X85000L,0X85400L,
- 0X4020L,0X4420L,0X5020L,0X5420L,0X84020L,0X84420L,0X85020L,0X85420L,
- 0X800L,0XC00L,0X1800L,0X1C00L,0X80800L,0X80C00L,0X81800L,0X81C00L,
- 0X820L,0XC20L,0X1820L,0X1C20L,0X80820L,0X80C20L,0X81820L,0X81C20L,
- 0X4800L,0X4C00L,0X5800L,0X5C00L,0X84800L,0X84C00L,0X85800L,0X85C00L,
- 0X4820L,0X4C20L,0X5820L,0X5C20L,0X84820L,0X84C20L,0X85820L,0X85C20L,
-
- 0X0L,0X100L,0X40000L,0X40100L,0X0L,0X100L,0X40000L,0X40100L,
- 0X40L,0X140L,0X40040L,0X40140L,0X40L,0X140L,0X40040L,0X40140L,
- 0X400000L,0X400100L,0X440000L,0X440100L,0X400000L,0X400100L,0X440000L,0X440100L,
- 0X400040L,0X400140L,0X440040L,0X440140L,0X400040L,0X400140L,0X440040L,0X440140L,
- 0X8000L,0X8100L,0X48000L,0X48100L,0X8000L,0X8100L,0X48000L,0X48100L,
- 0X8040L,0X8140L,0X48040L,0X48140L,0X8040L,0X8140L,0X48040L,0X48140L,
- 0X408000L,0X408100L,0X448000L,0X448100L,0X408000L,0X408100L,0X448000L,0X448100L,
- 0X408040L,0X408140L,0X448040L,0X448140L,0X408040L,0X408140L,0X448040L,0X448140L,
- 0X8L,0X108L,0X40008L,0X40108L,0X8L,0X108L,0X40008L,0X40108L,
- 0X48L,0X148L,0X40048L,0X40148L,0X48L,0X148L,0X40048L,0X40148L,
- 0X400008L,0X400108L,0X440008L,0X440108L,0X400008L,0X400108L,0X440008L,0X440108L,
- 0X400048L,0X400148L,0X440048L,0X440148L,0X400048L,0X400148L,0X440048L,0X440148L,
- 0X8008L,0X8108L,0X48008L,0X48108L,0X8008L,0X8108L,0X48008L,0X48108L,
- 0X8048L,0X8148L,0X48048L,0X48148L,0X8048L,0X8148L,0X48048L,0X48148L,
- 0X408008L,0X408108L,0X448008L,0X448108L,0X408008L,0X408108L,0X448008L,0X448108L,
- 0X408048L,0X408148L,0X448048L,0X448148L,0X408048L,0X408148L,0X448048L,0X448148L
- };
-
- /* --- some bit manipulation primitives --- */
-
- /* g_keybit -- extract bit bnum from the key in inkey */
- #define g_keybit(bnum) (01 & (inkey [bnum/8] >> (8 - (bnum%8))))
-
- long lrot28 (lval)
- /* do left rotate of 28 bit quantity */
- long lval;
- {
- lval <<= 1;
- if (0X10000000L & lval) lval++;
- lval &= 0XFFFFFFFL;
- return (lval);
- }
-
- /* --- following code and data does key schedule generation --- */
-
- #define SUCCESS 0 /* return codes for ksmake */
- #define FAILURE SUCCESS + 1
-
- int ksmake (inkey,ks) /* Make a key schedule from key bytes in inkey */
- unsigned inkey [8];
- unsigned ks [16] [4];
- {
- extern long pc2otab [8] [128];
-
- int round;
- long pcct, pcdt;
- long pc2out [2];
- int i, j, parac;
- unsigned incopy;
-
- /* test parity of bytes in inkey */
- for (i = 0; i < 8; i++) /* do each byte */
- {
- parac = 0;
- incopy = inkey [i];
-
- for (j = 0; j < 8; j++) /* 8 bits in a DES byte */
- {
- if (incopy & 01) parac++;
- incopy >>= 1;
- }
- if (! (parac & 01)) return (FAILURE); /* no odd parity on this byte */
- }
-
- /* do pc-1 permutation, extracting bits from inkey */
- pcct = pcdt = 0L;
- for (i = 0; i < 27; i++)
- /* filling all appropriate bits */
- {
- pcct |= g_keybit(pc1c [i]);
- pcdt |= g_keybit(pc1d [i]);
- pcct <<= 1;
- pcdt <<= 1;
- }
- /* one final ior, without a shift */
- pcct |= g_keybit(pc1c [i]);
- pcdt |= g_keybit(pc1d [i]);
-
- for (round = 0; round < 16; round++)
- {
- /* always at least one shift */
- pcct = lrot28 (pcct);
- pcdt = lrot28 (pcdt);
-
- if (shiftsked [round] == 2)
- { /* this round needs another shift */
- pcct = lrot28 (pcct);
- pcdt = lrot28 (pcdt);
- }
-
- /* Now, pcct and pcdt have the values on which we can apply
- pc2 and select the key bits, storing them in pc2out[0] and
- pc2out[1]. The high order [pc2out[0]] bits all come from
- pcct, and the low order from pcdt. */
-
- pc2out [0] = pc2otab [0] [pcct >> 21];
- pc2out [0] |= pc2otab [1] [0X7F & (pcct >> 14)];
- pc2out [0] |= pc2otab [2] [0X7F & (pcct >> 7)];
- pc2out [0] |= pc2otab [3] [0X7F & pcct];
-
- pc2out [1] = pc2otab [4] [pcdt >> 21];
- pc2out [1] |= pc2otab [5] [0X7F & (pcdt >> 14)];
- pc2out [1] |= pc2otab [6] [0X7F & (pcdt >> 7)];
- pc2out [1] |= pc2otab [7] [0X7F & pcdt];
-
- /* order key bits and bytes so as to be compatible with
- the format generated by the E implementation */
-
- ks [round] [0] = 0XFC00 & (pc2out [0] >> 8);
- ks [round] [0] |= (0XFC & (pc2out [0] >> 4));
- ks [round] [1] = 0XFC00 & (pc2out [1] >> 8);
- ks [round] [1] |= (0XFC & (pc2out [1] >> 4));
-
- ks [round] [2] = 0XFC00 & (pc2out [0] >> 2);
- ks [round] [2] |= (0XFC & (pc2out [0] << 2));
- ks [round] [3] = 0XFC00 & (pc2out [1] >> 2);
- ks [round] [3] |= (0XFC & (pc2out [1] << 2));
- }
- return (SUCCESS);
- }
-
- #define LMASK 0XFCFCFCFCL;
-
- /* doip -- permute 64 bits from inar to outar */
- doip (inar, outar)
- long inar [2];
- long outar [2];
- {
- long lt;
-
- outar [0] = inar [0];
- outar [1] = inar [1];
- EXSHMSK(outar[1],0x0f0f0f0f,outar[0],4,lt);
- EXSHMSK(outar[1],0x0000ffff,outar[0],16,lt);
- EXSHMSK(outar[0],0x33333333,outar[1],2,lt);
- EXSHMSK(outar[0],0x00ff00ff,outar[1],8,lt);
- EXSHMSK(outar[1],0x55555555,outar[0],1,lt);
- }
-
- /* doipi -- perform ip-inverse */
- doipi (inar, outar)
- long inar [2];
- long outar [2];
- {
- long lt;
-
- outar [0] = inar [0];
- outar [1] = inar [1];
- EXSHMSK(outar[1],0x55555555,outar[0],1,lt);
- EXSHMSK(outar[0],0x00ff00ff,outar[1],8,lt);
- EXSHMSK(outar[0],0x33333333,outar[1],2,lt);
- EXSHMSK(outar[1],0x0000ffff,outar[0],16,lt);
- EXSHMSK(outar[1],0x0f0f0f0f,outar[0],4,lt);
- }
-
- /* des_encrypt -- encrypt a block under key sched in ks */
- des_encrypt (inar, outar, ks)
- long inar [2];
- long outar [2];
- unsigned ks [16] [4];
- {
- extern long snop [8] [64];
-
- register int round, oddbit;
- unsigned expan [4]; /* receives output of E transform */
- long sbout, scopy, tlong;
- long oarr [2];
-
- doip (inar, outar);
-
- for (round = 0; round < 16; round++)
- {
- sbout = 0L;
-
- tlong = outar [1];
- oddbit = (int) (01 & tlong);
- tlong >>= 1;
- tlong &= 0X7FFFFFFFL; /* defeat sign extend -- jl 17 feb 84 */
- if (oddbit) tlong |= 0X80000000L;
-
- tlong &= LMASK;
- expan [0] = (unsigned) (tlong >> 16);
- expan [1] = (unsigned) tlong;
-
- tlong = outar [1];
- oddbit = !! (tlong & 0X80000000L);
- tlong <<= 3;
- if (oddbit) tlong |= 04;
- tlong &= LMASK;
- expan [2] = (unsigned) (tlong >> 16);
- expan [3] = (unsigned) tlong;
-
- /* this code bypasses the alternative of loop setup
- and resultant computation within the loop for speed */
- expan [0] ^= ks [round] [0];
- sbout |= snop [0] [0X3F & (expan [0] >> 10)];
- sbout |= snop [1] [0X3F & (expan [0] >> 2)];
- expan [1] ^= ks [round] [1];
- sbout |= snop [2] [0X3F & (expan [1] >> 10)];
- sbout |= snop [3] [0X3F & (expan [1] >> 2)];
- expan [2] ^= ks [round] [2];
- sbout |= snop [4] [0X3F & (expan [2] >> 10)];
- sbout |= snop [5] [0X3F & (expan [2] >> 2)];
- expan [3] ^= ks [round] [3];
- sbout |= snop [6] [0X3F & (expan [3] >> 10)];
- sbout |= snop [7] [0X3F & (expan [3] >> 2)];
-
- scopy = outar [0];
- outar [0] = outar [1];
- outar [1] = scopy ^ sbout;
- }
-
- /* a final swap */
- scopy = outar [0];
- outar [0] = outar [1];
- outar [1] = scopy;
-
- doipi (outar, oarr); /* perform ip-inverse into temp copy */
- outar [0] = oarr [0];
- outar [1] = oarr [1];
- }
-
- /* des_decrypt -- decrypt a block under key sched in ks */
- /* not used as of 5 May, but retained for completeness */
-
- des_decrypt (inar, outar, ks)
- long inar [2];
- long outar [2];
- unsigned ks [16] [4];
- {
- extern long snop [8] [64];
-
- register int round, oddbit;
- unsigned expan [4]; /* receives output of E transform */
- long sbout, scopy, tlong;
- long oarr [2];
-
- doip (inar, outar);
-
- for (round = 15; round >= 0; round--)
- /* note that decryption selects schedule keys in opposite order */
- {
- sbout = 0L;
-
- tlong = outar [1];
- oddbit = (int) (01 & tlong);
- tlong >>= 1;
- tlong &= 0X7FFFFFFFL; /* defeat sign extend -- jl 17 feb 84 */
- if (oddbit) tlong |= 0X80000000L;
-
- tlong &= LMASK;
- expan [0] = (unsigned) (tlong >> 16);
- expan [1] = (unsigned) tlong;
-
- tlong = outar [1];
- oddbit = !! (tlong & 0X80000000L);
- tlong <<= 3;
- if (oddbit) tlong |= 04;
- tlong &= LMASK;
- expan [2] = (unsigned) (tlong >> 16);
- expan [3] = (unsigned) tlong;
-
- /* this code bypasses the alternative of loop setup
- and resultant computation within the loop for speed */
- expan [0] ^= ks [round] [0];
- sbout |= snop [0] [0X3F & (expan [0] >> 10)];
- sbout |= snop [1] [0X3F & (expan [0] >> 2)];
- expan [1] ^= ks [round] [1];
- sbout |= snop [2] [0X3F & (expan [1] >> 10)];
- sbout |= snop [3] [0X3F & (expan [1] >> 2)];
- expan [2] ^= ks [round] [2];
- sbout |= snop [4] [0X3F & (expan [2] >> 10)];
- sbout |= snop [5] [0X3F & (expan [2] >> 2)];
- expan [3] ^= ks [round] [3];
- sbout |= snop [6] [0X3F & (expan [3] >> 10)];
- sbout |= snop [7] [0X3F & (expan [3] >> 2)];
-
- scopy = outar [0];
- outar [0] = outar [1];
- outar [1] = scopy ^ sbout;
- }
-
- /* a final swap */
- scopy = outar [0];
- outar [0] = outar [1];
- outar [1] = scopy;
-
- doipi (outar, oarr); /* perform ip-inverse into temp copy */
- outar [0] = oarr [0];
- outar [1] = oarr [1];
- }
- /* support routines for demodes */
-
- /* read input bytes from stdin */
- int g_inbytes (nby, buffer) /* returns TRUE iff EOF reached */
- int nby; /* number of bytes needed at a time */
- /* if insufficient, will be padded with nulls */
- char buffer [];
- {
- int bufix;
- int getcval;
-
- /* init all significant bytes to null pad */
- for (bufix = 0; bufix < nby; bufix++) buffer[bufix] = 0;
-
- for (bufix = 0; bufix < nby; bufix++)
- {
- getcval = getchar ();
- buffer [bufix] = (char) getcval;
- if (getcval == EOF)
- {
- buffer[bufix] = 0; /* replace with pad */
- return (TRUE); /* indicating that EOF detected */
- }
- }
- return (FALSE); /* indicating no EOF detected */
- }
-
- /* represent a series of bytes (conditionally) in two buffers, as
- printable representation and hex representation */
- di_bytes (nby, inbuf, pr_buf, hx_buf)
- int nby;
- char inbuf [];
- char pr_buf []; /* NULL if not desired */
- char hx_buf []; /* NULL if not desired */
- {
- int i;
-
- for (i = 0; i < nby; i++)
- {
- if (pr_buf != NULL)
- {
- /* Try to show as a printable character, either directly or */
- /* by masking off the MSB (if the latter, prefix with '#') */
- if (inbuf [i] > 0x7f)
- {
- if (isprint (inbuf [i] & 0x7f))
- sprintf (pr_buf, "#%c", inbuf [i] & 0x7f);
- else sprintf (pr_buf, " ");
- }
- else
- {
- if (isprint (inbuf [i]))
- sprintf (pr_buf, " %c", inbuf [i]);
- else sprintf (pr_buf, " ");
- }
- pr_buf += 2;
- }
- if (hx_buf != NULL)
- {
- sprintf (hx_buf, "%1x%1x", 0xf & (inbuf[i]>>4),0xf & inbuf [i]);
- hx_buf += 2;
- }
- }
- if (pr_buf != NULL) sprintf (pr_buf, "|");
- if (hx_buf != NULL) sprintf (hx_buf, "|");
- }
-
- packbtol (as_ba, as_l) /* pack 4 bytes into a long */
- char as_ba [];
- long *as_l;
- {
- long lt;
-
- lt = as_ba [0] & 0xff;
- lt <<= 8;
- lt |= as_ba [1] & 0xff;
- lt <<= 8;
- lt |= as_ba [2] & 0xff;
- lt <<= 8;
- lt |= as_ba [3] & 0xff;
-
- *as_l = lt;
- }
-
- sprdltob (as_l, as_ba) /* spread a long into 4 bytes */
- long as_l;
- char as_ba [];
- {
- as_ba [0] = (char) ((as_l >> 24) & 0xff);
- as_ba [1] = (char) ((as_l >> 16) & 0xff);
- as_ba [2] = (char) ((as_l >> 8) & 0xff);
- as_ba [3] = (char) (as_l & 0xff);
- }
-
- lsh64 (lar, count) /* left shift a 64-bit quantity maintained */
- long lar[]; /* in a 2-element long array. Assumes that */
- int count; /* a long has at least 32 bits */
- {
- int i;
- long t;
-
- for (i = 0; i < count; i++)
- {
- t = (lar [1] >> 31) & 0x1l;
- lar [1] <<= 1;
- lar [0] <<= 1;
- lar [0] |= t;
- }
- lar [0] &= 0xffffffffl;
- lar [1] &= 0xffffffffl;
- }
-
- rsh64 (lar, count) /* right shift a 64-bit quantity maintained */
- long lar[]; /* in a 2-element long array. Assumes that */
- int count; /* a long has at least 32 bits */
- {
- int i;
- long t;
-
- for (i = 0; i < count; i++)
- {
- t = lar [0] & 0x1l;
- lar [1] >>= 1;
- lar [0] >>= 1;
- lar [1] |= (t << 31);
- }
- }
-
- rplr64 (base, modr, nbits) /* replace rightmost nbits of base */
- long base[]; /* (64-bit qty in 2-elmt long array) */
- long modr[]; /* with corresponding bits from modr */
- int nbits;
- { /* works on architectures where a long is 32 bits or more */
- long mask;
- long lmod; /* local copy of modr word */
-
- mask = (nbits >= 32) ? 0xffffffffl : ((0x1l << nbits) - 1);
- /* mask = (0x1l << ((nbits > 32) ? 32 : nbits)) - 1; */
- base [1] &= ~mask; /* wipe out existing low bits */
- lmod = modr [1] & mask;
- base [1] |= lmod;
-
- nbits -= 32;
- if (nbits <= 0) return;
-
- mask = (0x1l << nbits) - 1;
- base [0] &= ~mask;
- lmod = modr [0] & mask;
- base [0] |= lmod;
- }
-
- cpy64 (src, dest) /* copy src 2-elmt long array to dest */
- long src[];
- long dest[];
- {
- dest [0] = src [0];
- dest [1] = src [1];
- }
-
- #ifndef TIMEONLY
- main (argc,argv)
- int argc;
- char *argv [];
- {
- int argn;
- int dmode;
- int eofyet = FALSE; /* will set to TRUE when end of input */
- int quant = 0; /* quantum size (in bits) for selected mode */
- int flwid; /* size to display a 64-bit chunk of pt or ct */
- int i, j;
- long iv [2], keyl [2];
-
- unsigned inkey [8];
- char inkeyc [8];
- char inbbuf [8], otbbuf [8];
- char iprline [80], ihxline [80], ohxline [80];
- unsigned ks [16] [4];
-
- long plain [2], crypt [2], cprbuf [2], cipher [2];
-
- /* default key and IV to NBS sample values */
- keyl [0] = 0x01234567l;
- keyl [1] = 0x89abcdefl;
- iv [0] = 0x12345678l;
- iv [1] = 0x90abcdefl;
-
- /* scan the command line */
- for (argn = 1; argn <= argc; argn++)
- {
- if (seq_nocase (argv[argn], "-m")) /* mode specifier */
- { /* one string argument expected */
- argn++;
- if (seq_nocase (argv[argn], "ecb"))
- {
- dmode = ECB; quant = 64;
- }
- else if (seq_nocase (argv[argn], "cbc"))
- {
- dmode = CBC; quant = 64;
- }
- else if (seq_nocase (argv[argn], "ofb"))
- {
- dmode = OFB; if (quant == 0) quant = 64; /* may be overridden */
- }
- else if (seq_nocase (argv[argn], "cfb"))
- {
- dmode = CFB; if (quant == 0) quant = 64; /* may be overridden */
- }
- }
- else if (seq_nocase (argv[argn], "-k")) /* key specifier */
- { /* two long hex numerics expected */
- argn++;
- sscanf (argv[argn], "%lx", &keyl [0]);
- argn++;
- sscanf (argv[argn], "%lx", &keyl [1]);
- }
- else if (seq_nocase (argv[argn], "-iv")) /* IV specifier */
- { /* two long hex numerics expected */
- argn++;
- sscanf (argv[argn], "%lx", &iv [0]);
- argn++;
- sscanf (argv[argn], "%lx", &iv [1]);
- }
- else if (seq_nocase (argv[argn], "-q")) /* quantum specifier */
- { /* one decimal number expected */
- argn++;
- sscanf (argv[argn], "%d", &quant);
- }
- }
-
- if (((dmode == ECB) || (dmode == CBC)) && (quant != 64))
- {
- printf ("Can't override quantum of 64 for ECB or CBC modes... aborting\n");
- exit (1);
- }
-
- /* build key schedule (outside scanner, so default works) */
- sprdltob (keyl [0], & inkeyc [0]);
- sprdltob (keyl [1], & inkeyc [4]);
- for (i = 0; i < 8; i++) inkey [i] = (unsigned) (inkeyc [i] & 0xff);
- if (ksmake (inkey, ks) != SUCCESS)
- {
- printf ("Bad parity on key... aborting...\n");
- exit (1);
- }
-
- printf ("Mode is ");
- switch (dmode)
- {
- case ECB:
- printf ("ECB;");
- break;
- case CBC:
- printf ("CBC;");
- break;
- case OFB:
- printf ("OFB;");
- printf (" Quantum is %d;", quant);
- break;
- case CFB:
- printf ("CFB;");
- printf (" Quantum is %d;", quant);
- break;
- default:
- printf ("Unknown;");
- break;
- }
- printf (" Key is %lx %lx;", keyl [0], keyl [1]);
- if (dmode != ECB) printf (" IV is %lx %lx;", iv [0], iv [1]);
- printf ("\n");
-
- flwid = (8 * 2) + 1; /* 8 bytes, 2 chars/byte, and a delimiter */
-
- while (eofyet == FALSE) /* until EOF detected on input */
- {
- if (dmode == ECB)
- {
- for (i = 0; i < 4; i++) /* 4 groups on a printable line */
- {
- if (TRUE == g_inbytes (8, inbbuf)) eofyet = TRUE;
- di_bytes (8, inbbuf, &iprline [flwid*i], &ihxline [flwid*i]);
- packbtol (&inbbuf[0], &plain[0]);
- packbtol (&inbbuf[4], &plain[1]);
- des_encrypt (plain, crypt, ks);
- sprdltob (crypt[0], &otbbuf[0]);
- sprdltob (crypt[1], &otbbuf[4]);
- di_bytes (8, otbbuf, NULL, &ohxline [flwid*i]);
- if (TRUE == eofyet) break;
- }
- }
- else if (dmode == CBC)
- {
- for (i = 0; i < 4; i++) /* 4 groups on a printable line */
- {
- if (TRUE == g_inbytes (8, inbbuf)) eofyet = TRUE;
- di_bytes (8, inbbuf, &iprline [flwid*i], &ihxline [flwid*i]);
- packbtol (&inbbuf[0], &plain[0]);
- packbtol (&inbbuf[4], &plain[1]);
- plain [0] ^= iv [0]; /* XOR IV with input plaintext */
- plain [1] ^= iv [1];
- des_encrypt (plain, crypt, ks);
- sprdltob (crypt[0], &otbbuf[0]);
- sprdltob (crypt[1], &otbbuf[4]);
- di_bytes (8, otbbuf, NULL, &ohxline [flwid*i]);
- iv [0] = crypt [0]; /* use ciphertext output as next IV */
- iv [1] = crypt [1];
- if (TRUE == eofyet) break;
- }
- }
- else if (dmode == CFB)
- {
- for (i = 0; i < 4; i++) /* 4 groups on a printable line */
- /* does file read and display in 64-bit units regardless of quantum */
- {
- if (TRUE == g_inbytes (8, inbbuf)) eofyet = TRUE;
- di_bytes (8, inbbuf, &iprline [flwid*i], &ihxline [flwid*i]);
- packbtol (&inbbuf[0], &plain[0]);
- packbtol (&inbbuf[4], &plain[1]);
- for (j = 0; j < 64; j+=quant)
- {
- des_encrypt (iv, crypt, ks);
- rsh64 (crypt, 64-quant); /* right-align active DES output bits */
- cpy64 (plain, cprbuf); /* copy plaintext to scratch */
- rsh64 (cprbuf, 64-(quant+j)); /* right-align active plaintext bits */
- /* XOR them into DES output */
- crypt [0] ^= cprbuf [0]; crypt [1] ^= cprbuf [1];
- lsh64 (iv, quant); /* shift existing IV over */
- rplr64 (iv, crypt, quant); /* replace LS IV bits with DES output */
- lsh64 (cipher, quant); /* make space in cipher out array */
- rplr64 (cipher, crypt, quant); /* insert new output bits */
- }
- sprdltob (cipher[0], &otbbuf[0]);
- sprdltob (cipher[1], &otbbuf[4]);
- di_bytes (8, otbbuf, NULL, &ohxline [flwid*i]);
- if (TRUE == eofyet) break;
- }
- }
- else if (dmode == OFB)
- {
- for (i = 0; i < 4; i++) /* 4 groups on a printable line */
- {
- if (TRUE == g_inbytes (8, inbbuf)) eofyet = TRUE;
- di_bytes (8, inbbuf, &iprline [flwid*i], &ihxline [flwid*i]);
- packbtol (&inbbuf[0], &plain[0]);
- packbtol (&inbbuf[4], &plain[1]);
- for (j = 0; j < 64; j+=quant)
- {
- des_encrypt (iv, crypt, ks);
- rsh64 (crypt, 64-quant); /* right-align active DES output bits */
- lsh64 (iv, quant); /* shift existing IV over */
- rplr64 (iv, crypt, quant); /* replace LS IV bits with DES output */
- cpy64 (plain, cprbuf); /* copy plaintext to scratch */
- rsh64 (cprbuf, 64-(quant+j)); /* right-align active plaintext bits */
- /* XOR them into DES output */
- crypt [0] ^= cprbuf [0]; crypt [1] ^= cprbuf [1];
- lsh64 (cipher, quant); /* make space in cipher out array */
- rplr64 (cipher, crypt, quant); /* insert new output bits */
- }
- sprdltob (cipher[0], &otbbuf[0]);
- sprdltob (cipher[1], &otbbuf[4]);
- di_bytes (8, otbbuf, NULL, &ohxline [flwid*i]);
- if (TRUE == eofyet) break;
- }
- }
- printf ("%s\n%s\n%s\n", iprline, ihxline, ohxline);
- }
- printf ("\n");
- }
- #endif
-
- #ifdef TIMEONLY
- main ()
- {
- int itnum, i;
- long plain [2], crypt [2], cprbuf [2];
- long udiff, bitsdone, u_msec;
- unsigned ks [16] [4];
- long keyl [2];
- unsigned inkey [8];
- char inkeyc [8];
-
- /* default key to NBS sample value */
- keyl [0] = 0x01234567l;
- keyl [1] = 0x89abcdefl;
- /* build key schedule (outside scanner, so default works) */
- sprdltob (keyl [0], & inkeyc [0]);
- sprdltob (keyl [1], & inkeyc [4]);
- for (i = 0; i < 8; i++) inkey [i] = (unsigned) (inkeyc [i] & 0xff);
- if (ksmake (inkey, ks) != SUCCESS)
- {
- printf ("Bad parity on key... aborting...\n");
- exit (1);
- }
-
- printf ("Enter number of test iterations [decimal]: ");
- scanf ("%d", &itnum);
-
- times (&t_before);
- for (i = 0; i < itnum; i++)
- {
- des_encrypt (plain, crypt, ks);
- des_decrypt (crypt, cprbuf, ks);
- if ((plain [0] != cprbuf [0]) || (plain [1] != cprbuf [1]))
- {
- printf ("Encrypt and decrypt disagree -- aborting!!\n");
- exit (1);
- }
- }
- times (&t_after);
- printf ("All encryptions and decryptions verified consistent\n");
-
- /* note: the following code won't work if one times a number
- of encryptions that take less than one tick, as can occur
- on a vax with a small number of tests */
- udiff = t_after.proc_utime - t_before.proc_utime;
- printf ("user 1/60 sec ticks = %ld\n", udiff);
- bitsdone = 2 * (itnum * 64);
- u_msec = (udiff * 1000) / 60;
- printf ("%ld user msec/DES cycle\n", u_msec / ((long) 2 * itnum));
- printf ("%ld bits/user second\n", (bitsdone * 1000) / u_msec);
- }
- #endif
-